//
//  VistorLoginView.swift
//  SinaMicroBlog
//
//  Created by wxy on 15/9/2.
//  Copyright © 2015年 wxy. All rights reserved.
//

import UIKit

class VistorLoginView: UIView {
    
    func setUpInfo(imageName: String?, message: String) {
        
        messgaeLabel.text = message
        //文字居中
        messgaeLabel.textAlignment = NSTextAlignment(rawValue: 1)!
        
        if let imgName = imageName {
            
            iconImageView.image = UIImage(named: imgName)
            
            //隐藏房子
            houseImageView.hidden = true
            //隐藏遮罩视图
            sendSubviewToBack(maskImageView)
        }else{
            
            startAnimation()
        }
    }
    
/// 首页动画效果
    private func startAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        animation.duration = 5
        animation.repeatCount = MAXFLOAT
        
        //设置动画不被删除，保证跳转回界面，保持动画效果
        //当iconImageView销毁时，动画会被自动释放
        animation.removedOnCompletion = false
        
        iconImageView.layer.addAnimation(animation, forKey: nil)
        
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 布局访客界面
    private func setUpUI() {
        
        addSubview(iconImageView)
        //遮罩视图添加有顺序
        addSubview(maskImageView)
        addSubview(houseImageView)
        addSubview(messgaeLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        //默认禁止使用自动布局，需要关闭
        //1.旋转图标自动布局
        iconImageView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -60))
        
        //2.小房子
        houseImageView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: houseImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: houseImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //3.消息文字
        messgaeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: messgaeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: messgaeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        //设置固定值width
        //参照物为nil，Attribute设置:NSLayoutAttribute.NotAnAttribute
        addConstraint(NSLayoutConstraint(item: messgaeLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        
        //4.注册按钮
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messgaeLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messgaeLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //5.登陆按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messgaeLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messgaeLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //6.遮罩视图 maskImageView 利用VFL可视化布局语言
        maskImageView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v" : maskImageView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-(-35)-[registerButton]", options: [], metrics: nil, views: ["v" :maskImageView,"registerButton" :registerButton]))
        
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)        
        
    }
    
    //MARK: - 懒加载控件
    //旋转图标
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //小房子
    private lazy var houseImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //旋转图标效果（maskImageView）
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //消息文字
    private lazy var messgaeLabel: UILabel = {
        
        let messageLabel = UILabel()
        
        messageLabel.text = "关注一些人，回这里看看有什么惊喜"
        messageLabel.textColor = UIColor.darkGrayColor()
        messageLabel.font = UIFont.systemFontOfSize(14)
        messageLabel.numberOfLines = 0
        
        return messageLabel
    }()
    
    lazy var registerButton: UIButton = {
        
        let registerBtn = UIButton()
        
        registerBtn.setTitle("注册", forState: UIControlState.Normal)
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        return registerBtn
    }()
    
    lazy var loginButton: UIButton = {
       
        let loginBtn = UIButton()
        
        loginBtn.setTitle("登陆", forState: UIControlState.Normal)
        loginBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        return loginBtn
    }()
    
    
}
